package org.example.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.domain.*;
import org.example.dto.GoodsDetailDTO;
import org.example.mapper.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GoodsService {

    @Resource
    private SkuMapper skuMapper;

    @Resource
    private SpuMapper spuMapper;

    @Resource
    private SpuSkuRelationMapper spuSkuRelationMapper;

    @Resource
    private CategoryGoodsRelationMapper categoryGoodsRelationMapper;

    @Resource
    private AttributeMapper attributeMapper;

    @Resource
    private AttributeEnumMapper attributeEnumMapper;

    @Resource
    private CategoryAttrRelationMapper categoryAttrRelationMapper;

    @Resource
    private BrandMapper brandMapper;

    @Resource
    private CategoryBrandRelationMapper categoryBrandRelationMapper;

    @Resource
    private GoodsMediaMapper goodsMediaMapper;

    public GoodsDetailDTO detail(Long skuId) {
        GoodsDetailDTO result = new GoodsDetailDTO();

        SkuDO skuDO = skuMapper.selectById(skuId);
        result.setSku(skuDO);

        LambdaQueryWrapper<SpuSkuRelationDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SpuSkuRelationDO::getSkuId, skuId);
        SpuSkuRelationDO spuSkuRelationDO = spuSkuRelationMapper.selectOne(lambdaQueryWrapper);

        SpuDO spuDO = spuMapper.selectById(spuSkuRelationDO.getSpuId());
        result.setSpu(spuDO);

        BrandDO brandDO = brandMapper.selectById(spuDO.getBrandId());
        result.setBrand(brandDO);

        LambdaQueryWrapper<GoodsMediaDO> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GoodsMediaDO::getSkuId, skuId);
        List<GoodsMediaDO> goodsMediaDOS = goodsMediaMapper.selectList(lqw);
        result.setMediaList(goodsMediaDOS);
        return result;
    }
}
